package com.xing.controller;

import com.xing.entity.pojo.Article;
import com.xing.entity.pojo.ArticleType;
import com.xing.entity.vo.ArticleTypePage;
import com.xing.service.ArticleTypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

//期间还干哈翁达外噶外感干亨汉生胶天涯热土遇到突发
@Controller
@RequestMapping("articleType")
public class ArticleTypeConterller {

    @Autowired
    private ArticleTypeService articleTypeService;

    @RequestMapping("/findAllCount")
    public String findAllCount(Model model, @RequestParam(value = "typeState",required = false,defaultValue = "3") Byte typeState,
                                            @RequestParam(value = "typeName",required = false) String typeName,
                                            @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                            @RequestParam(value = "rows",required = false,defaultValue = "3") Integer rows){
        ArticleTypePage page = articleTypeService.findByPage(currentPage, rows, typeName, typeState);
        model.addAttribute("typeName",typeName);
        model.addAttribute("typeState",typeState);
        model.addAttribute("page",page);

        return "articleType/articleType";
    }


    @RequestMapping("/deleteSelectedArticle")
    @ResponseBody
    public String deleteSelectedArticle(@RequestParam("ids")  String[] ids){
        String msg;
        Boolean aBoolean = articleTypeService.deleteckArticle(ids);
        if(aBoolean){
            msg="删除成功！";
        }else {
            msg="删除失败！";
        }
        return msg;
    }

    @RequestMapping("/findByTitle/{typeId}/{currentPage}")
    public String findByTitle(@PathVariable("typeId") String typeId,Model model,@PathVariable("currentPage") Integer currentPage){
        if(typeId.equals("add")){
            return "articleType/addArticleType";
        }else {
            ArticleType type = articleTypeService.selectByPrimaryKey(typeId);
            model.addAttribute("type",type);
            return "articleType/updateArticleType";
        }
    }

    @PostMapping("/updateArticleType")
    public String updateArticleType(ArticleType articleType,Integer currentPage){
        articleTypeService.updateByPrimaryKeySelective(articleType);
        return "redirect:/articleType/findAllCount?currentPage="+currentPage;
    }

    @PostMapping("/addArticleType")
    public String addArticleType(ArticleType articleType){
        articleType.setTypeId(UUID.randomUUID().toString());
        articleTypeService.insertSelective(articleType);
        return "redirect:/articleType/findAllCount";
    }
}
